﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebMvc.App.Web.Default.oracleTrans
{
    public class insertAction  : Controller
	{
		public insertAction(System.IO.TextWriter tw):base(tw){}
		public insertAction(string fileName) : base(fileName) {}
		public insertAction(HttpContext context):base(context){}
		public insertAction(Microsoft.Owin.IOwinContext context):base(context){}
        
        public void insert(string name,string pwd)
        {
            var con = new Oracle.ManagedDataAccess.Client.OracleConnection(NFinal.Config.ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString);
			con.Open();
            var trans = con.BeginTransaction();
            int id = 0;
            try
            {
                #region	var id; 插入并返回ID
			var __insert_id_NonQuery = 0;
			var __insert_id_command__ = new Oracle.ManagedDataAccess.Client.OracleCommand("insert into users(name,pwd) values(:name,:pwd)", con);
			__insert_id_command__.Transaction=trans;
			var __insert_id_parameters__=new Oracle.ManagedDataAccess.Client.OracleParameter[2];
			__insert_id_parameters__[0] = new Oracle.ManagedDataAccess.Client.OracleParameter(":name",Oracle.ManagedDataAccess.Client.OracleDbType.Varchar2,20);
			__insert_id_parameters__[0].Value = name;
			__insert_id_parameters__[1] = new Oracle.ManagedDataAccess.Client.OracleParameter(":pwd",Oracle.ManagedDataAccess.Client.OracleDbType.Varchar2,20);
			__insert_id_parameters__[1].Value = pwd;
			__insert_id_command__.Parameters.AddRange(__insert_id_parameters__);
			try
			{
				__insert_id_NonQuery = __insert_id_command__.ExecuteNonQuery();
				__insert_id_command__.Dispose();
				if(__insert_id_NonQuery>0)
				{
					__insert_id_command__ = new Oracle.ManagedDataAccess.Client.OracleCommand("select users_id_sequence.currval  from dual", con);
					__insert_id_command__.Transaction=trans;
					id = Convert.ToInt32(__insert_id_command__.ExecuteScalar());
					__insert_id_command__.Dispose();
				}
			}
			catch
			{
				__insert_id_command__.Dispose();
			}
			#endregion
			
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
            }
            con.Close();
            Write(id);
        }
        
        
        
        
        
        
        
    }
}